package com.example.dao;

import com.example.model.ManagementUser;
import com.example.utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ManagementUserDAO {
    // 查询所有用户
    public List<ManagementUser> findAllManagementUsers() {
        List<ManagementUser> users = new ArrayList<>();
        String sql = "SELECT * FROM management_user";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                ManagementUser user = new ManagementUser();
                user.setManagementId(rs.getInt("management_id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setName(rs.getString("name"));
                user.setDepartmentId(rs.getInt("department_id"));
                user.setContactInfo(rs.getString("contact_info"));
                user.setCreateTime(rs.getTimestamp("create_time"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    // 根据ID查询用户
    public ManagementUser findManagementUserById(int id) {
        ManagementUser user = null;
        String sql = "SELECT * FROM management_user WHERE management_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                user = new ManagementUser();
                user.setManagementId(rs.getInt("management_id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setName(rs.getString("name"));
                user.setDepartmentId(rs.getInt("department_id"));
                user.setContactInfo(rs.getString("contact_info"));
                user.setCreateTime(rs.getTimestamp("create_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    // 模糊搜索查询
    public List<ManagementUser> searchManagementUsers(String keyword) {
        List<ManagementUser> users = new ArrayList<>();
        String sql = "SELECT * FROM management_user WHERE username LIKE ? ESCAPE '!' OR name LIKE ? ESCAPE '!'";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, "%" + keyword + "%");
            pstmt.setString(2, "%" + keyword + "%");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                ManagementUser user = new ManagementUser();
                user.setManagementId(rs.getInt("management_id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setName(rs.getString("name"));
                user.setDepartmentId(rs.getInt("department_id"));
                user.setContactInfo(rs.getString("contact_info"));
                user.setCreateTime(rs.getTimestamp("create_time"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    // 添加用户
    public boolean addManagementUser(ManagementUser user) {
        String sql = "INSERT INTO management_user (username, password, name, department_id, contact_info) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getName());
            pstmt.setInt(4, user.getDepartmentId());
            pstmt.setString(5, user.getContactInfo());
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 更新用户
    public boolean updateManagementUser(ManagementUser user) {
        String sql = "UPDATE management_user SET username = ?, password = ?, name = ?, department_id = ?, contact_info = ? WHERE management_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getName());
            pstmt.setInt(4, user.getDepartmentId());
            pstmt.setString(5, user.getContactInfo());
            pstmt.setInt(6, user.getManagementId());
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除用户
    public boolean deleteManagementUser(int id) {
        String sql = "DELETE FROM management_user WHERE management_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}